package Noyau;

import java.awt.geom.Point2D;
import java.lang.Math;
import java.util.List;
import java.util.ArrayList;

public abstract class CheminChariot extends Chemin implements InterfaceChariot
{
	protected List<Chariot> chariots = new ArrayList<Chariot> ();
	protected List<InterfaceChariot> entrees = new ArrayList<InterfaceChariot>();
	protected List<InterfaceChariot> sorties = new ArrayList<InterfaceChariot>();
    
    public List<Chariot> getChariots ()  {
        return this.chariots;
    }

	@Override
	public List<InterfaceChariot> getEntrees() {
		return this.entrees;
	}

	@Override
	public List<InterfaceChariot> getSorties() {
		return this.sorties;
	}
	
	@Override
	public void addEntree(InterfaceChariot value)
	{
		if (this.entrees.contains(value))
		{
			return;
		}
		
		this.entrees.add(value);
		
		this.setChanged();
		this.notifyObservers();
	}
	
	@Override
	public void addSortie(InterfaceChariot value)
	{
		if (this.sorties.contains(value))
		{
			return;
		}
		
		this.sorties.add(value);
		value.addEntree(this);
		
		this.setChanged();
		this.notifyObservers();
	}

	@Override
	public void CreerChariot(Chariot chariot) {
		chariot.setParent(this);		
		this.chariots.add(chariot);
		
		this.setChanged();
		this.notifyObservers();
	}
	
	@Override
    public List<Chemin> getNeighbors()
    {
		List<Chemin> result = new ArrayList<Chemin>();
		
		for (InterfaceChariot sortie : this.sorties)
		{
			result.add((Chemin)sortie);
		}
		
		return result;
    }
	
	@Override
	public double getAngle()
	{
		double result = 0;
		
		if (this.getVisualSize() == 0)
		{
			if (this.entrees.size() > 1 && this.sorties.size() == 1)
			{
				result = this.sorties.get(0).getAngle();
			}
			else if (this.entrees.size() == 1 && this.sorties.size() > 1)
			{
				result = this.entrees.get(0).getAngle();
			}
			else if (this.entrees.size() > 0 && this.sorties.size() > 0)
			{
				result = (this.entrees.get(0).getAngle() + this.sorties.get(0).getAngle()) / 2;
			}
		}
		else
		{
			if (this.entrees.size() != 0)
			{				
				Point2D firstEntreeOrigin = this.entrees.get(0).getOrigine();
				Point2D vecteur = new Point2D.Double(this.getOrigine().getX() - firstEntreeOrigin.getX(), this.getOrigine().getY() - firstEntreeOrigin.getY());
				
				result = Math.atan2(vecteur.getY(), vecteur.getX()) + Math.PI;
			}
		}
		
		return result;
	}
	
	@Override
	public List<ObjetMetier> getChildren()
	{
		List<ObjetMetier> children = new ArrayList<ObjetMetier>();
		
		children.addAll(this.chariots);
		
		return children;
	}
	
	@Override
	public void SupprimerChariot(Chariot chariot)
	{
		if (!this.chariots.contains(chariot))
		{
			return;
		}
		
		this.chariots.remove(chariot);
		this.setChanged();
		this.notifyObservers();
	}
}
